package com.yylending.las.lending.quota.idcard;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IEvent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Global;
import com.yylending.las.common.AESEntity;
import com.yylending.las.Constants;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by laixj on 2016/11/18.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class IdCardPresenter extends Presenter<IdCardModel, IdCardPresenter.View> {
	private final static String TAG = "IdCardPresenter";

	public interface View extends IView {
		/**
		 * 保存身份证信息
		 */
		void saveIdCardSuccess();

		void saveIdCardFail(String msg, boolean exit);
	}

	public static class Event extends IEvent {
		public final static int ID_CARD_RESCAN = 6;

		public Event(int eventId) {
			id = eventId;
		}
	}

	public static class Generator implements PresenterFactory<IdCardPresenter> {
		@Override
		public IdCardPresenter create() {
			return new IdCardPresenter();
		}
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new IdCardModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/**
	 * 保存身份证信息
	 */
	void saveIdCardInfo(IdCardModel.IdCard idCard) {
		execute(new AESEntity(idCard), IdCardModel.METHOD_SAVE_ID_CARD, false, idCard.iname, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void saveIdCardResult(IdCardModel.SaveIdCardResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				Global.LOGIN_REAL_NAME = (String) result.extraParam;
				mView.saveIdCardSuccess();
			} else {
				boolean exit = false;
				String msg = Constants.MSG_SAVE_ID_CARD_FAILED;
				if (Constants.CODE_700000.equals(result.getRetCode())) {
					exit = true;
					msg = Constants.MSG_700000;
				} else if (Constants.CODE_700050.equals(result.getRetCode())) {
					exit = true;
					msg = Constants.MSG_700050;
				} else if (Constants.CODE_700051.equals(result.getRetCode())) {
					exit = true;
					msg = Constants.MSG_700051;
				} else if (Constants.CODE_711111.equals(result.getRetCode())) {
					exit = true;
					msg = Constants.MSG_711111;
				} else if (Constants.CODE_700030.equals(result.getRetCode())) {
					exit = true;
					msg = Constants.MSG_700030;
				}
				mView.saveIdCardFail(msg, exit);
			}
		}
	}
}